<template>
  <div class="login-wrapper">
    <!-- Left Side: Brand & Visual -->
    <div class="login-brand">
      <div class="brand-content">
        <div class="logo-circle">
          <el-icon :size="40" color="#4f46e5"><DataAnalysis /></el-icon>
        </div>
        <h1 class="brand-title">TeamPlan360</h1>
        <p class="brand-subtitle">智能团队协作与计划管理平台</p>
        <div class="brand-features">
          <div class="feature-item">
            <el-icon><Calendar /></el-icon>
            <span>年度/月度/周计划全流程管理</span>
          </div>
          <div class="feature-item">
            <el-icon><TrendCharts /></el-icon>
            <span>智能生成工作报告与总结</span>
          </div>
          <div class="feature-item">
            <el-icon><User /></el-icon>
            <span>清晰的团队协作与权限体系</span>
          </div>
        </div>
      </div>
      <div class="brand-bg-circle c1"></div>
      <div class="brand-bg-circle c2"></div>
    </div>

    <!-- Right Side: Login Form -->
    <div class="login-form-container">
      <div class="login-card">
        <div class="form-header">
          <h2>欢迎回来</h2>
          <p>请登录您的账号以继续</p>
        </div>

        <el-form :model="loginForm" :rules="rules" ref="loginFormRef" size="large" label-position="top">
          <el-form-item label="用户名" prop="username">
            <el-input 
              v-model="loginForm.username" 
              placeholder="请输入用户名" 
              :prefix-icon="User"
            />
          </el-form-item>
          
          <el-form-item label="密码" prop="password">
            <el-input 
              v-model="loginForm.password" 
              type="password" 
              placeholder="请输入密码"
              :prefix-icon="Lock"
              show-password
              @keyup.enter="handleLogin"
            />
          </el-form-item>
          
          <el-form-item>
            <el-button type="primary" @click="handleLogin" :loading="loading" class="submit-btn">
              登录系统
            </el-button>
          </el-form-item>
        </el-form>
        
        <div class="demo-credentials-card">
          <p class="demo-title">演示账号</p>
          <div class="credential-row">
            <span class="role-badge manager">经理</span>
            <span class="code">manager / manager123</span>
          </div>
          <div class="credential-row">
            <span class="role-badge employee">员工</span>
            <span class="code">employee / employee123</span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { useAuthStore } from '../stores/auth'
import { ElMessage } from 'element-plus'
import { User, Lock, DataAnalysis, Calendar, TrendCharts } from '@element-plus/icons-vue'

export default {
  name: 'Login',
  components: {
    DataAnalysis, Calendar, TrendCharts
  },
  setup() {
    const router = useRouter()
    const authStore = useAuthStore()
    
    const loginForm = ref({
      username: '',
      password: ''
    })
    
    const loading = ref(false)
    const loginFormRef = ref()
    
    const rules = {
      username: [
        { required: true, message: '请输入用户名', trigger: 'blur' }
      ],
      password: [
        { required: true, message: '请输入密码', trigger: 'blur' }
      ]
    }
    
    const handleLogin = async () => {
      if (!loginFormRef.value) return
      
      await loginFormRef.value.validate(async (valid) => {
        if (valid) {
          loading.value = true
          try {
            await authStore.login(loginForm.value.username, loginForm.value.password)
            ElMessage.success('登录成功')
            
            if (authStore.isManager) {
              router.push('/manager')
            } else {
              router.push('/employee')
            }
          } catch (error) {
            ElMessage.error(error)
          } finally {
            loading.value = false
          }
        }
      })
    }
    
    return {
      loginForm,
      rules,
      loading,
      loginFormRef,
      handleLogin,
      User,
      Lock
    }
  }
}
</script>

<style scoped>
.login-wrapper {
  display: flex;
  min-height: 100vh;
  width: 100%;
  overflow: hidden;
}

/* Left Side */
.login-brand {
  flex: 1;
  background: linear-gradient(135deg, var(--primary-color) 0%, #4338ca 100%);
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  padding: 40px;
  overflow: hidden;
}

.brand-content {
  position: relative;
  z-index: 2;
  max-width: 480px;
}

.logo-circle {
  width: 80px;
  height: 80px;
  background: white;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 24px;
  box-shadow: 0 10px 25px rgba(0,0,0,0.2);
}

.brand-title {
  font-size: 3rem;
  font-weight: 800;
  margin-bottom: 12px;
  letter-spacing: -1px;
}

.brand-subtitle {
  font-size: 1.25rem;
  opacity: 0.9;
  margin-bottom: 48px;
  font-weight: 300;
}

.brand-features {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.feature-item {
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: 1.1rem;
  opacity: 0.9;
}

.brand-bg-circle {
  position: absolute;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.1);
  z-index: 1;
}

.c1 {
  width: 600px;
  height: 600px;
  top: -100px;
  right: -100px;
}

.c2 {
  width: 400px;
  height: 400px;
  bottom: -50px;
  left: -50px;
}

/* Right Side */
.login-form-container {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #fff;
  padding: 40px;
}

.login-card {
  width: 100%;
  max-width: 420px;
}

.form-header {
  margin-bottom: 40px;
}

.form-header h2 {
  font-size: 2rem;
  font-weight: 700;
  color: var(--text-main);
  margin-bottom: 8px;
}

.form-header p {
  color: var(--text-secondary);
}

.submit-btn {
  width: 100%;
  height: 48px;
  font-size: 1.1rem;
  margin-top: 10px;
}

.demo-credentials-card {
  margin-top: 40px;
  padding: 20px;
  background: #f9fafb;
  border-radius: var(--border-radius);
  border: 1px solid #f3f4f6;
}

.demo-title {
  font-size: 0.875rem;
  color: var(--text-secondary);
  margin-bottom: 12px;
  font-weight: 600;
}

.credential-row {
  display: flex;
  align-items: center;
  margin-bottom: 8px;
  font-size: 0.9rem;
}

.credential-row:last-child {
  margin-bottom: 0;
}

.role-badge {
  padding: 2px 8px;
  border-radius: 4px;
  font-size: 0.75rem;
  font-weight: 600;
  margin-right: 12px;
  width: 50px;
  text-align: center;
}

.role-badge.manager {
  background: #e0e7ff;
  color: #4338ca;
}

.role-badge.employee {
  background: #d1fae5;
  color: #059669;
}

.code {
  font-family: monospace;
  color: var(--text-main);
}

/* Responsive */
@media (max-width: 900px) {
  .login-wrapper {
    flex-direction: column;
  }
  
  .login-brand {
    flex: 0 0 200px;
    padding: 20px;
  }
  
  .logo-circle {
    width: 50px;
    height: 50px;
    margin-bottom: 12px;
  }
  
  .brand-title {
    font-size: 2rem;
    margin-bottom: 4px;
  }
  
  .brand-subtitle {
    font-size: 1rem;
    margin-bottom: 0;
  }
  
  .brand-features {
    display: none;
  }
}
</style>
